package com.wzh.houseEs.conf;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.wzh.common.utils.R;
import com.wzh.houseEs.feign.HouseFeign;
import com.wzh.houseEs.vo.NewHouseVo;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @author WZH
 * @title: EsRuner
 * @projectName find_house
 * @description:
 * @date 2022/3/24
 */
@Configuration
public class EsRuner implements ApplicationRunner {

    @Autowired
    private HouseFeign houseFeign;

    @Autowired
    private RestHighLevelClient client;

    @Override
    public void run(ApplicationArguments args) throws Exception {
//        R r = houseFeign.esNewList();
//        if (r.getCode()==0){
//            List<NewHouseVo> RentHouses = r.getData(new TypeReference<List<NewHouseVo>>() {
//            });
//            BulkRequest request = new BulkRequest();
//            RentHouses.stream().forEach(item->{
//                IndexRequest indexRequest = new IndexRequest("renthouse");
//                indexRequest.id(item.getHouseId().toString());
//                String jsonString = JSON.toJSONString(item);
//                indexRequest.source(jsonString, XContentType.JSON);
//                request.add(indexRequest);
//            });
//            BulkResponse bulkResponse = client.bulk(request, RequestOptions.DEFAULT);
//        }
    }
}
